package findpath_system

import (
	"confs/gen/common"
	"game/app/actors/schemas"
	"game/app/systems/math_system/math_utils"
)

func init() {

}

// FindPathSystem 寻路系统
type FindPathSystem struct{}

type IFindPathSystem interface {
	GetFindPath(state *schemas.FindPathState, start *common.Vector2D, end *common.Vector2D) []*common.Vector2D
}

var FindPath IFindPathSystem = FindPathSystem{}

// GetFindPath 获取寻路点
func (f FindPathSystem) GetFindPath(state *schemas.FindPathState, start *common.Vector2D, end *common.Vector2D) []*common.Vector2D {
	// return []*common.Vector2D{end}
	// logger.WARN("FindPathSystem", start, end)
	startPos := math_utils.MakeVector2dByProto(start)
	endPos := math_utils.MakeVector2dByProto(end)
	ok, result, size := state.Recast.FindPath([]float32{startPos.X.ToFloat32(), 0.0, startPos.Y.ToFloat32()},
		[]float32{endPos.X.ToFloat32(), 0.0, endPos.Y.ToFloat32()})
	if !ok {
		return nil
	}

	paths := []*common.Vector2D{}
	for i := 0; i < size; i++ {
		x := math_utils.MakeZNumberByFloat32(result[i*3])
		y := math_utils.MakeZNumberByFloat32(result[i*3+2])
		pos := math_utils.MakeVector2dByZNumber(x, y)
		paths = append(paths, pos.ToVector2D())
	}
	//logger.WARN("findpath result:", paths[1:])
	return paths
}
